package com.bidevalution.business.vo;

import com.alibaba.dubbo.common.json.JSON;
import com.bidevalution.dao.entity.ExpertWarnMsgVo;
import lombok.*;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @version 1.6
 * @author whc
 * @修改 ZTKP-4025，增加专家确认无误缓存
 */
/**
 * {
 * 	"msg": "你有投标单位总评分偏离平均分30%以上，请确认是否有误！",
 * 	"button": {
 * 		"left": {
 * 			"show": true,
 * 			"name": "取消",
 * 		    "urlAction": "Cancel"
 *                }，
 * 		"right": {
 * 			"show": true,
 * 			"name": '确认无误',
 * 			  "urlAction": "Confime"
 *        }* 	},
 * 	"failureMap": {
 * 		"0ff52245-8bff-422c-b9c2-f07eeec520ce": "hbh2",
 * 		"26359dc9-d1c5-452a-99ec-ab859fa65ca9": "dmj4",
 * 		"95fda736-527e-4552-891c-38fc977be6b8": "dmj1"* 	},
 * 	"experTotal": 3,
 * 	"details": {
 * 		"0ff52245-8bff-422c-b9c2-f07eeec520ce": ["5d5da57c-6f32-4f45-b506-1bfb17da793a", "8db46067-95b2-42ae-aff6-57a3ce79f9b7"],
 * 		"26359dc9-d1c5-452a-99ec-ab859fa65ca9": ["5d5da57c-6f32-4f45-b506-1bfb17da793a", "8db46067-95b2-42ae-aff6-57a3ce79f9b7"],
 * 		"95fda736-527e-4552-891c-38fc977be6b8": ["5d5da57c-6f32-4f45-b506-1bfb17da793a", "8db46067-95b2-42ae-aff6-57a3ce79f9b7"]* 	}
 * }
 */
@Getter
@Setter
public class CheckJobVo {
    private String msg;
    private Map<ButtonEnum, Button> buttonMap = new HashMap<>();
    /**
     * 阻碍提醒，需要回滚提交得专家ID
     */
    private Map<String, String> failureMap = new HashMap<>();
    private Integer experTotal = 0; // 总专家数
    private Map<String, Object> details = new HashMap<>();

    public CheckJobVo() {
    }

    public CheckJobVo addBtton(ButtonEnum buttonEnum, String name, String buttonAction, String hide) {
        return addBtton(buttonEnum, name,
                buttonAction == null ? buttonEnum.defauleAction : ButtonActionEnum.valueOf(buttonAction),
                hide);
    }

    public CheckJobVo addBtton(ButtonEnum buttonEnum, String name, ButtonActionEnum buttonActionEnum, String hide) {
        Button button = new Button(buttonEnum, name, buttonActionEnum,hide);
        buttonMap.put(button.getButtonEnum(), button);
        return this;
    }

    public void add(ExpertWarnMsgVo expertWarnMsgVo) {
        String expertName = expertWarnMsgVo.getExpertName();
        String expertId = expertWarnMsgVo.getExpertId();
        String enterpriseId = expertWarnMsgVo.getEnterpriseId();

        details(expertId, enterpriseId);
        failureMap.put(expertId, expertName);
    }

    private void details(String expertId, String enterpriseId) {
        List<String> list = (List<String>) details.get(expertId);
        if (list == null) {
            list = new ArrayList<>();
        }
        list.add(enterpriseId);

        details.put(expertId, list);
    }

    public void addFailure(String expId, String expertName) {
        failureMap.put(expId, expertName);
    }

    public void setDetails(Map<String, Object> details) {
        this.details = details;
    }

    public void addDetails(String expertId, String expertName,Object details) {
        if (details != null &&
                ((details instanceof List && ((List) details).size() > 0 ) ||
                 (details instanceof Map && ((Map) details).size() > 0 )
                )
        ) {
            this.failureMap.put(expertId, expertName);
            this.details.put(expertId, details);
        }
    }

    public void removeDetails(String expertId) {
        this.failureMap.remove(expertId);
        this.details.remove(expertId);
    }

    /**
     * 按钮
     */
    @Data
    class Button {
        private ButtonEnum buttonEnum;
        private String name;
        private ButtonActionEnum urlAction;
        private boolean show;

        public Button(ButtonEnum buttonEnum, String name, ButtonActionEnum urlAction, String hide) {
            this.buttonEnum = buttonEnum;
            this.urlAction = urlAction == null ? buttonEnum.defauleAction : urlAction;
            this.name = name == null? buttonEnum.defaultName : name;
            this.show = !Boolean.valueOf(hide);
        }

        @Override
        public String toString() {
            return String.format("\"%s\" : {" +
                    "\"show\": %s," +
                    "\"name\": \"%s\"," +
                    "\"urlAction\": \"%s\"" +
                    "} ", buttonEnum.getName(), show, name, buttonEnum.name());
        }
    }



    /**
     * 提醒弹框按钮类型，目前只有两个； 后期如果是多个可以考虑列表，不过正常情况下只有两个，就统一名称好了
     */
    @AllArgsConstructor
    public enum ButtonEnum {
        Left("取消", ButtonActionEnum.Cancel),
        Right("确认无误", ButtonActionEnum.Confirm);
        public String defaultName;
        public ButtonActionEnum defauleAction;

        private String getName() {
            return this.name().toLowerCase();
        }

        @Override
        public String toString() {
            return getName();
        }
    }

    /**
     * checkJob 按钮actionUrl,前端会根据名字进行对应的url请求
     */
    @AllArgsConstructor
    public enum ButtonActionEnum {
        Cancel, Confirm,UpdateBasePrice,Sort;
    }
}
